import {defineStore} from 'pinia'
import {Dict} from "@utils/resource";

export const commonStore = defineStore({
    id: 'common',
    state: () => ({
        loading: false,
        dicts: []
    }),
    persist: true,
    getters: {
        systemName: (state) => {
            let ts = state.dicts.filter(t => t.code === 'system');
            if (ts.length > 0) {
                return ts[0].value;
            }
            return 'sking-admin'
        }
    },
    actions: {
        async loadDict(code) {
            if (code) {
                console.log(this.dicts)
                let res = await Dict.list({
                    code
                });
                this.dicts.forEach((dict, index) => {
                    if (dict.code === code) {
                        this.dicts.splice(index, 1)
                    }
                })
                this.dicts = [...this.dicts, ...res.data]
                console.log(this.dicts)
            } else {
                let res = await Dict.list();
                this.dicts = res.data
            }
        },
    }
})
